<template>
    <div class="address-box">
        <van-nav-bar title="地址管理" fixed left-text="返回" left-arrow @click-left="goBack">
            <template #right>
                <van-icon name="fire-o" color="#ee0a24" size="20"/>
            </template>
        </van-nav-bar>
        <div class="address-item">
            <van-address-list
                    v-if="from != 'mine'"
                    v-model="chosenAddressId"
                    :list="addressList"
                    default-tag-text="默认"
                    @add="onAdd"
                    @edit="onEdit"
                    @select="select"
            />
            <van-address-list
                    v-else
                    v-model="chosenAddressId"
                    :list="addressList"
                    default-tag-text="默认"
                    @add="onAdd"
                    @edit="onEdit"
            />
        </div>
    </div>
</template>

<script>
    import {Toast} from 'vant';
    import sHeader from '../../components/SimpleHeader'
    import {getAddressList} from '../../api/address'

    export default {
        name: "userAddress",
        components: {
            sHeader
        },
        data() {
            return {
                chosenAddressId: '1',
                addressList: [],
                from: this.$route.query.from,
            }
        },
        async mounted() {
            this.getList();
        },
        methods: {
            getList() {
                getAddressList().then(res => {
                    const result = res.data;
                    result.map(item => {
                        const reAddress = item.province + item.city + item.region + item.detailAddress;
                        this.addressList.push(
                            {
                                id: item.id,
                                name: item.name,
                                tel: item.phoneNumber,
                                address: reAddress,
                                isDefault: item.defaultStatus === 1 ? true : false
                            }
                        )
                    })
                })
            },
            goBack(){
                this.$router.go(-1)
            },
            onAdd() {
                this.$router.push({path: `address-edit?type=add&from=${this.from}`})
            },
            onEdit(item, index) {
                this.$router.push({path: `address-edit?type=edit&addressId=${item.id}&from=${this.from}`})
            },
            select(item, index) {
                this.$router.push({path: `create-order?addressId=${item.id}&from=${this.from})}`})
            }
        }
    }
</script>

<style lang="less">
    @import '../../common/style/mixin';

    .address-box {
        .van-radio__icon {
            display: none;
        }

        .address-item {
            margin-top: 44px;

            .van-button {
                background: @primary;
                border-color: @primary;
            }
        }
    }
</style>